package com.coollang.smartbra.fragment;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.MainActivity;
import com.coollang.smartbra.base.BaseFragment;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.FontsUtil;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.views.OptionCircle;
import com.coollang.smartbra.views.WaveView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;


/**
 * 侧滑栏页面
 * 侧滑栏打开  判断蓝牙是否打开关闭
 */
public class DrawerLayoutFragment extends BaseFragment {
    @BindView(R.id.ll_heart_rate_alarm)
    LinearLayout llHeartRateAlarm;

    /**
     * 健身计划
     */
    @BindView(R.id.ll_fitness_plan)
    LinearLayout llFitnessPlan;

    /**
     * 动作库
     */
    @BindView(R.id.ll_action_libary)
    LinearLayout llActionLibary;

    /**
     * 运动统计
     */
    @BindView(R.id.ll_sport_statistics)
    LinearLayout llSportStatis;

    @BindView(R.id.status_bar)
    View statusBar;

    /**
     * 水波纹View
     */
    @BindView(R.id.waveview)
    WaveView mWaveView;

    /**
     * 圆圈
     */
    @BindView(R.id.cicleview)
    public OptionCircle mOptionCircle;

    /**
     * 蓝牙信息
     */
    @BindView(R.id.ll_ble_info)
    LinearLayout mLlBleInfo;
    @BindView(R.id.tv_ble_info)
    TextView mTvBleInfo;

    /**
     * 电量信息ui提示
     */
    @BindView(R.id.tv_battery_info)
    TextView mTVBatteryInfo;
    @BindView(R.id.tv_battery_count)
    TextView mTVBatteryCount;

    Unbinder unbinder;

    private int mBattery;
    private String mDeviceName;
    private String mVersionCode;
    private SpannableString msp;

    @Override
    public View inflateView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_drawer_layout, null);
    }

    @Override
    public void initView(View view) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        unbinder = ButterKnife.bind(this, view);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar);
        ((MainActivity) getActivity()).setDrawerLayoutFragment(this);

    }

    private void bleDisConnectUI() {
        mLlBleInfo.setVisibility(View.GONE);
        // 设置小圆点开始转动
        mOptionCircle.setCircleVisible(true);
        mOptionCircle.showText(true);
        // mOptionCircle.startCountdown();

        mTVBatteryInfo.setVisibility(View.GONE);
        mTVBatteryCount.setVisibility(View.GONE);

        setWaveView(false);
    }

    private void setWaveView(boolean isShowWave) {
        mWaveView.setVisibility(isShowWave ? View.VISIBLE : View.GONE);

        if (isShowWave) {
            mWaveView.setShowWave(true);
            mLlBleInfo.setVisibility(View.VISIBLE);

            mWaveView.setAmplitudeRatio(0.03f);
            mWaveView.setWaveLengthRatio(0.8f);

            mWaveView.setBorder(0, getActivity().getResources().getColor(R.color.tra));

            // 设置小圆圈
            mOptionCircle.setCircleVisible(false);
            mOptionCircle.showText(false);
            mTVBatteryInfo.setVisibility(View.VISIBLE);
            mTVBatteryCount.setVisibility(View.VISIBLE);

            mBattery = MyApplication.getApplication().betery;
            // 主要调这里
            mWaveView.setWaterLevelRatio(mBattery / 100.0f);

            int frontWaveColor;

            if (mBattery > 20) {
                frontWaveColor = getActivity().getResources().getColor(R.color.ble_electric_color_1);
            } else {
                frontWaveColor = getActivity().getResources().getColor(R.color.ble_electric_color_2);
            }

            mWaveView.setWaveColor(getActivity().getResources().getColor(R.color.tra), frontWaveColor);

            setBatteryCount(mBattery + "%");
        }
    }

    private void setBatteryCount(String text) {
        msp = new SpannableString(text);
        msp.setSpan(new RelativeSizeSpan(2.0f), 0, text.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(FontsUtil.getInstance().getMyNumTypefaceSpan(), 0, text.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTVBatteryCount.setText(msp);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void onEventMainThread(CommonEvent event) {
        if (event.type == CommonEvent.DISPALYALL) {
            switch (event.what) {
                case CommonEvent.REQUESTFAILUE:
                    break;
                case CommonEvent.REQUESTSUEECCD:
                    setBeteryDisplay();
                    break;
                default:
                    break;
            }
        }
    }

    public void setBeteryDisplay() {

        // 固件版本号
        mVersionCode = "固件版本: " + MyApplication.getApplication().version;
        mDeviceName = "序列号: " + MyApplication.getApplication().deviceName;

        mTvBleInfo.setText(mDeviceName + "\n" + mVersionCode);

        // 设置水波纹
        setWaveView(true);
    }

    @Override
    public void initData() {
    }

    public void setBleinfo() {

        boolean isConnected = MyApplication.getApplication().isConnected;
        if (!isConnected) {
            //如果处于断开连接状态
            // 蓝牙未连接UI
            bleDisConnectUI();
        } else {
            // 设置水波纹
            setWaveView(true);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.cicleview, R.id.ll_heart_rate_alarm, R.id.ll_fitness_plan, R.id.ll_action_libary, R.id.ll_sport_statistics})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cicleview:
                if (mOptionCircle.getCircleVisible()) {
                    ActivitySwitcher.goSearchAct(getActivity());
                } else {
                    return;
                }
                break;
            case R.id.ll_heart_rate_alarm:
                if (MyApplication.getApplication().isConnected) {
                    ActivitySwitcher.goSetHeartRateAct(getActivity());
                } else {
                    ToastUtils.showToast("当前未连接设备");
                }
                break;
            case R.id.ll_fitness_plan:
                // 健身计划
                if (MyApplication.getApplication().hasNet()) {
                    ActivitySwitcher.goToSportPlan(getActivity());
                }
                break;
            case R.id.ll_action_libary:
                // 动作库
                ActivitySwitcher.goViconDataAct(getActivity());
                break;
            case R.id.ll_sport_statistics:
                // 运动统计
                if (MyApplication.getApplication().hasNet()) {
                    ActivitySwitcher.goToSportAnalyze(getActivity());
                }
                break;
            default:
                break;
        }
        ((MainActivity) getActivity()).mDrawerLayout.closeDrawers();
    }
}
